package com.centerserver;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;

class RecvSequentialNaluThread extends Thread {
	
	private DatagramSocket mServerSocket;
	private DatagramPacketQueue mDatagramPacketQueue;
	
	public RecvSequentialNaluThread(DatagramPacketQueue datagramPacketQueue) {
		
		mDatagramPacketQueue = datagramPacketQueue;
		try {
			mServerSocket = new DatagramSocket(CenterServerConfig.CAPTURE_END_SENDTO_RTP_PORT);
		} catch (SocketException e) {
			System.out.println("SocketException in RecordVideoThread#RecvSequentialNaluSessionThread");
			//e.printStackTrace();
		}
	}

	@Override
	public synchronized void run() {
		
//		System.out.println("Debug: " + "RecvSequentialNaluThread started");

		// to receive one RTP packet

		while (true) {

			byte[] rtpPacket = new byte[CenterServerConfig.RTP_PACKET_MAX_SIZE];
			DatagramPacket rtpDatagram = new DatagramPacket(rtpPacket,
					rtpPacket.length);
			
			try {
				mServerSocket.receive(rtpDatagram);
			} catch (IOException e) {
				System.out.println("IOException in RecordVideoThread");
				//e.printStackTrace();
			}
			
//			System.out.println("Debug: ReceiveSequentialNalu received one RTP packet");
//			System.out.println("Debug: add len:" + rtpDatagram.getLength());
			mDatagramPacketQueue.add(rtpDatagram);
			
//			System.out.println("Debug: ReceiveSequentialNalu add one packet to Queue!!!");
		}
	}
}